#include <vector>
using namespace std;
class Solution
{
public:
    int minCost(vector<vector<int>> &costs)
    {
        int size = costs.size();
        vector<vector<int>> dp(size, vector<int>(3));
        dp[0] = costs[0];
        int pev_red(dp[0][0]), pev_green(dp[0][1]), pev_blue(dp[0][2]);
        // 动态计算每个房子分别粉刷每种颜色的最小价格
        // 这个房子粉刷一个中颜色等于前一个房子两外两个颜色的最小价格
        for (int i = 1; i < size; i++)
        {
            dp[i][0] = costs[i][0] + min(pev_green, pev_blue);
            dp[i][1] = costs[i][1] + min(pev_red, pev_blue);
            dp[i][2] = costs[i][2] + min(pev_red, pev_green);
            pev_red = dp[i][0], pev_green = dp[i][1], pev_blue = dp[i][2];
        }
        return min(min(pev_red, pev_green), pev_blue);
    }
};